package com.boang.controller.owners;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boang.entity.PO.Owner;
import com.boang.entity.VO.OwnerVO;
import com.boang.resp.Result;
import com.boang.service.OwnerService;
import com.boang.service.RoomService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/*
@author:22567
@date:2025/3/4 18:16
*/
@Tag(name = "业主管理owners", description = "业主管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/owners")
public class OwnerController {
    @Autowired
    private OwnerService ownerService;
    @Autowired
    private RoomService roomService;

    //-------------------查询--------------------
    @PostMapping("/search")
    @Operation(summary = "查询")
    public ResponseEntity<List<Owner>> searchOwners(@RequestBody OwnerVO queryDTO) {
        return ResponseEntity.ok(ownerService.queryOwners(queryDTO));
    }

    //-------------------查询--------------------
    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public IPage<Owner> page(@RequestBody OwnerVO queryDTO) {
        Page<Owner> page = new Page<>(
                queryDTO.getPageNumber() != null ? queryDTO.getPageNumber() : 1,  // 自动拆箱
                queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10);
        LambdaQueryWrapper<Owner> wrapper = new LambdaQueryWrapper<>();

        // 精确查询
        wrapper.eq(queryDTO.getOwnerId() != null, Owner::getOwnerId, queryDTO.getOwnerId());
        wrapper.eq(StringUtils.isNotBlank(queryDTO.getBuildingNumber()), Owner::getBuildingNumber, queryDTO.getBuildingNumber());
        wrapper.eq(StringUtils.isNotBlank(queryDTO.getIdNumber()), Owner::getIdNumber, queryDTO.getIdNumber());

        // 模糊查询
        wrapper.like(StringUtils.isNotBlank(queryDTO.getName()), Owner::getName, queryDTO.getName());
        wrapper.like(StringUtils.isNotBlank(queryDTO.getContact()), Owner::getContact, queryDTO.getContact());
        wrapper.like(StringUtils.isNotBlank(queryDTO.getMemberName()), Owner::getMemberName, queryDTO.getMemberName());
        wrapper.like(StringUtils.isNotBlank(queryDTO.getMemberPhone()), Owner::getMemberPhone, queryDTO.getMemberPhone());
        // 执行分页查询
        IPage<Owner> pageResult = ownerService.page(page, wrapper);
        List<Owner> owners = pageResult.getRecords();

        // 提取业主ID列表
        List<Long> ownerIds = owners.stream()
                .map(Owner::getOwnerId)
                .collect(Collectors.toList());

        // 批量查询房屋数量
        Map<Long, Long> houseCountMap = roomService.countHousesByOwnerIds(ownerIds);

        // 设置房屋数量到业主对象
        owners.forEach(owner -> {
            Long count = houseCountMap.getOrDefault(owner.getOwnerId(), 0L);
            owner.setHouseCount(count.intValue()); // 根据字段类型调整
        });

        // 更新分页结果
        pageResult.setRecords(owners);
        return pageResult;
    }


    //-------------------添加--------------------
    @PostMapping("/add")
    @Operation(summary = "添加")
    public Result<Boolean> add(@RequestBody Owner owner) {
        return Result.ok(ownerService.save(owner));
    }

    //-------------------修改--------------------
    @PutMapping("/update")
    @Operation(summary = "修改")
    public Result<Boolean> update(@RequestBody Owner owner) {
        return Result.ok(ownerService.updateById(owner));
    }
    //-------------------删除--------------------

    @DeleteMapping("/remove")
    @Operation(summary = "删除")
    public Result<Boolean> remove(int id) {
        return Result.ok(ownerService.removeById(id));
    }


}